//
//  ViewController.m
//  collection添加图片
//
//  Created by apple on 2019/6/4.
//  Copyright © 2019 铁科院. All rights reserved.
//

#import "ViewController.h"
#import "AddCollectionViewCell.h"
#import "ImageCollectionViewCell.h"
#define kImageCollectionViewCellID @"ImageCollectionViewCell"
#define kAddCollectionViewCellID @"AddCollectionViewCell"
//整个屏幕的宽
#define kScreenWidth [UIScreen mainScreen].bounds.size.width
//整个屏幕的高
#define kScreenHeight [UIScreen mainScreen].bounds.size.height
//适配机型比例
#define kWidthScale (kScreenWidth / 375.0)
#define kHeightScale (kScreenHeight / 667.0)
@interface ViewController ()<UICollectionViewDelegate,UICollectionViewDataSource,UINavigationControllerDelegate,UIImagePickerControllerDelegate>
@property (nonatomic, strong)UICollectionView *collectionView;

@property (nonatomic, strong)NSMutableArray *imageS;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self initCollectionView];
    // Do any additional setup after loading the view, typically from a nib.
}



#pragma mark--- UICollectionViewDelegate,UICollectionViewDataSource
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    if (self.imageS.count > 0) {
        if (self.imageS.count == 6) {
            return self.imageS.count;
        }
        return self.imageS.count + 1;
    }
    return 1;
}

- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row < self.imageS.count) {
         ImageCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:kImageCollectionViewCellID forIndexPath:indexPath];
        UIImage *image = self.imageS[indexPath.item];
        cell.image = image;
        cell.deleteButtonBlock = ^(UIButton * _Nonnull button) {
            
            [self.imageS removeObject:image];
            [self.collectionView reloadData];
        };
        return cell;
    }
    AddCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:kAddCollectionViewCellID forIndexPath:indexPath];
    cell.addImageBlock = ^(UIButton * _Nonnull button) {
        //初始化UIImagePickerController类
       UIImagePickerController * picker = [[UIImagePickerController alloc] init];
        //判断数据来源为相册
        picker.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
        //设置代理
        picker.delegate = self;
        //打开相册
     [self presentViewController:picker animated:YES completion:nil];
    };
    return cell;
}

//选择完成回调函数
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info{
    //获取图片
    UIImage *image = info[UIImagePickerControllerOriginalImage];
    [self.imageS addObject:image];
    [self.collectionView reloadData];
    [self dismissViewControllerAnimated:YES completion:nil];
}

//用户取消选择
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker{
    [self dismissViewControllerAnimated:YES completion:nil];
}


- (void)initCollectionView{
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
    layout.itemSize = CGSizeMake((kScreenWidth- 40)/2, kHeightScale *100);
    // 设置间距
    layout.sectionInset = UIEdgeInsetsMake(0, 10, 0, 10);
    layout.minimumLineSpacing = 5;
    layout.minimumInteritemSpacing = 5;
    _collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0,84+10, kScreenWidth, kScreenHeight) collectionViewLayout:layout];
    _collectionView.backgroundColor = [UIColor whiteColor];
    _collectionView.dataSource=self;
    _collectionView.delegate=self;
    [_collectionView registerNib:[UINib nibWithNibName:@"AddCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:kAddCollectionViewCellID];
    [_collectionView registerNib:[UINib nibWithNibName:@"ImageCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:kImageCollectionViewCellID];
    [self.view addSubview:self.collectionView];
}

- (NSMutableArray *)imageS{
    if (_imageS == nil) {
        _imageS = [NSMutableArray array];
    }
    return _imageS;
}


@end
